import org.kwis.msp.lcdui.Jlet;
import org.kwis.msp.lcdui.Display;
import org.kwis.msp.db.DataBase;
import org.kwis.msp.db.DataComparator;
import org.kwis.msp.db.DataFilterInteger;
import org.kwis.msp.db.DataFilter;
import org.kwis.msp.db.DataBaseException;
import org.kwis.msp.db.DataBaseRecordException;
import org.kwis.msp.io.FileSystem;

public class SortDataBase extends Jlet{
	
	public SortDataBase(){  }

	protected void startApp(String[] arg0){
		
		DataBase dataBASE3 = null;
		String dataBaseName="dataBASE3";
		int dbRecordLength=5;

		try	{	 

			dataBASE3 = DataBase.openDataBase(dataBaseName, dbRecordLength, true);

			String stringSource="WIPIDataBaseEX";

			int iteration = stringSource.length()-4;

			for(int i=0;i<iteration;i++)	{		   
				byte data[] = (stringSource.substring(i,i+5)).getBytes();
				dataBASE3.insertRecord(data,0,5);
			}
			
			databaseState(dataBASE3);
			
			if (dataBASE3.getNumberOfRecords() > 0) {
		        DBRecordCompare DBCompare = new DBRecordCompare();
				DBRecordFilter DBFilter = new DBRecordFilter();
				int index [] = dataBASE3.sortRecord(DBFilter, DBCompare);

				System.out.println("Sorted Record of "+dataBaseName);
				System.out.println(" ");

		       for (int i =0; i<index.length; i++) {
		         String str = new String(dataBASE3.selectRecord(index[i]));
		         System.out.println("Record "+index[i]+" : "+str);
			   }
			}

			dataBASE3.closeDataBase();

		} catch(Exception e){	}	
		
	}

	protected void destroyApp(boolean arg0) {  }

//== databaseState() ==//

	public void databaseState(DataBase db){


		try{

			System.out.println("===================================================");
			System.out.println("");

			System.out.println("DataBase Name = "+db.getDataBaseName() );

			int mode;
			mode=DataBase.getAccessMode(db.getDataBaseName());
			if(mode==FileSystem.PRIVATE_ACCESS){System.out.println("Access Mode = PRIVATE_ACCESS Mode");}				
			else if(mode==FileSystem.SHARED_ACCESS){System.out.println("Access Mode = SHARED_ACCESS Mode");}			
			else if(mode==FileSystem.SYSTEM_ACCESS){System.out.println("Access Mode = SYSTEM_ACCESS Mode");}

			System.out.println("DataBase Size = "+db.getDataBaseSize() +" Bytes");
			System.out.println("Record Number = "+db.getNumberOfRecords() );
			System.out.println("Record Size   = "+db.getRecordSize()+" Bytes");
			System.out.println("Last Modified Time = "+db.getLastModified());
			System.out.println("Available Size = " + db.getSizeAvailable()+" Bytes");
			System.out.println("");

			System.out.println("Records of "+db.getDataBaseName());
	
			
			
			for(int index=0;index<db.getNumberOfRecords();index++){
				try{
					System.out.println("Record "+ index+" : "+new String(db.selectRecord(index)));
				}catch(DataBaseRecordException dbre){System.out.println("DB Record Exception : " +index);}
			}		 

			System.out.println("");
			System.out.println("===================================================");

		}catch(Exception e){}
				
	}

	//== DataComparator ==/

	class DBRecordCompare implements DataComparator {
	 public int compare(byte[] rec1, byte[] rec2) {
		  String strREC1 = new String(rec1);
		  String strREC2 = new String(rec2);
		  int cmpValue = strREC1.compareTo(strREC2);
		  if (cmpValue == 0)
		   return DataComparator.EQUIVALENT;
		  else if (cmpValue < 0)
			return DataComparator.PRECEDES;
		  else
			  return DataComparator.FOLLOWS;          
		}
	}

//== DataFilter ==//

	class DBRecordFilter implements DataFilter {
	 public boolean filter(byte[] rec) {
		  int i = (int)rec[0];
		  if((i>=65)&&(i<=90)){return true;}
		  else{return false;}
		}
	}

}
